﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Customer_Profile.eCardReport.DBDao;
using KVB.Common;
using System.Text;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.eCardReport
{
    public partial class Pressure_report : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                string strYear = DateTime.Now.Year.ToString();
                dropYear.ClearSelection();
                dropYear.Items.FindByValue(strYear).Selected = true;
            }
            BindDataView();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private void BindDataView()
        {
            string startDate = dropYear.SelectedItem.Value+ "-01-01";
            string endDate = dropYear.SelectedItem.Value + "-12-31";
            string strSQL1 = "";
            try
            {
                //select '2012年8月' as SumDate,SUM(case when ifis_again!='Y' and convert(date,value_date) between '2012-09-01' and '2012-09-30' then real_count else 0 end) as in_money,
                //SUM(case when convert(date,due_date) between '2012-09-01' and '2012-09-30' then incard_count else 0 end) as out_money
                //from mmdeals where if_refund !='Y'

                strSQL1 = "";
                DataSet dsRefDate = ReportDao.getPressureRefDate(startDate,endDate);
                foreach (DataRow dr in dsRefDate.Tables[0].Rows)
                {
                    strSQL1 += "select '" + dr["ref_remark_date"] + "' as RefDate,SUM(case when convert(date,value_date) between '" + dr["ref_start_date"] + "' and '" + dr["ref_end_date"] + "' then initial_amount else 0 end) as in_money,";
                    strSQL1 += "SUM(case when convert(date,due_date) between '" + dr["ref_start_date"] + "' and '" + dr["ref_end_date"] + "' then due_amount else 0 end) as out_money ";
                    strSQL1 += "  from mmdeals where status='PASS' AND if_returncard='N'";
                    strSQL1 += " union all ";
                }

                strSQL1 = strSQL1.Substring(0, strSQL1.Length - 11);
                //strSQL1 += " from mmdeals where if_refund !='Y' ";
                //KVB.Common.Log.WriteDefaultLog("SQL:", strSQL1);
                string strSQL = "select RefDate,in_money,out_money,out_money-in_money as spread from (" + strSQL1 + ") v ";
                DataSet dsResult = ReportDao.DairySumReport(strSQL);
                if (dsResult != null && dsResult.Tables.Count > 0)
                {
                    //ASPxGridView1.Columns.Clear(); //清除列
                    //ASPxGridView1.AutoGenerateColumns = true; //设置自动添加列
                    //ASPxGridView1.DataSourceID = null;
                    ASPxGridView1.DataSource = dsResult.Tables[0];
                    ASPxGridView1.DataBind();
                    //ASPxGridView1.AutoGenerateColumns = false;
                }
                //else
                //{
                //    ASPxGridView1.DataBind();
                //}
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("Maturity_BindDataView", ex.ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.WriteXlsToResponse();
        }
    }
}